package com.gouwudai.gobbs.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IBlackListDAO;
import com.gouwudai.gobbs.pojo.TbbsBlackList;
import com.gouwudai.gobbs.service.IBlackListService;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.util.ExtremeTableUtil;
import com.litt.core.util.FormatDateTime;
import com.litt.core.util.ParamUtils;

/**
 * 
 * <pre>
 *  Title:分类/版面黑名单Service
 *  Description: 分类/版面黑名单Service 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-6-21
 * @version 1.0
 */
public class BlackListServiceImpl implements IBlackListService 
{
	IBlackListDAO blackListDAO;
    
	/**
	 * 保存黑名单
     * @param tbbsBlackList
	 */
	public void save(TbbsBlackList tbbsBlackList)
	{    		
        blackListDAO.save(tbbsBlackList);		
	}
    
    /**
     * 批量保存黑名单
     * @param tbbsBlackList
     */
    public void saveBatch(List tbbsBlackLists)
    {           
        for(int i=0;i<tbbsBlackLists.size();i++)
        {
            TbbsBlackList tbbsBlackList = (TbbsBlackList)tbbsBlackLists.get(i);
            blackListDAO.save(tbbsBlackList);       
        }
        
    }    
	
	/**
	 * 读取黑名单信息
     * @param id
	 * @return
	 */
	public TbbsBlackList load(Integer id) throws BusiException
	{
        TbbsBlackList tbbsBlackList = blackListDAO.load(id);
        if(tbbsBlackList==null)
            throw new BusiException("该黑名单信息已失效！");        
        return tbbsBlackList;
	} 
    
    /**
     * 更新黑名单
     * @param paramMap
     */
    public void update(Map paramMap)
    {           
        int blackId = ParamUtils.getMapToInt(paramMap, "blackId");        
        TbbsBlackList tbbsBlackList = blackListDAO.load(blackId);
        tbbsBlackList.setBlackType(ParamUtils.getMapToInt(paramMap, "blackType"));
        tbbsBlackList.setLimitTime(ParamUtils.getMapToInt(paramMap, "limitTime"));
        tbbsBlackList.setReason(ParamUtils.getMapToString(paramMap, "reason"));
        tbbsBlackList.setCreateDatetime(new Date());
        blackListDAO.update(tbbsBlackList);        
    }  
    
    /**
     * 批量删除黑名单
     * @param blackIds 多个黑名单ID
     */
    public void deleteBatch(String blackIds)
    {
        blackListDAO.deleteBatch(blackIds);
    }
    
    /**
     * 查询某一用户某一版面的黑名单
     * @param classId
     * @param userId
     * @return List
     */
    public List listUserClass(Integer classId,Integer userId)
    {
        return blackListDAO.listUserClass(classId, userId);     
    }
    
    /**
     * 表格控件绑定
     * @param request
     */
    public void listPageByClass(javax.servlet.http.HttpServletRequest request)
    {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("classId", Utility.parseInt(request.getParameter("classId")));
        
        Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = blackListDAO.listPageByClass(new PageParam(paramMap,limit.getPage(),limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
        request.setAttribute("resultList",page.getRsList());
        request.setAttribute("totalRows",page.getTotalSize());
    }
    
    /**
     * 判断当前用户在当前版面是否被指定的黑名单进行了限制.
     * 
     * @param classId the class id
     * @param userId the user id
     * @param blackType the black type
     * 
     * @return true, if is black
     */
    public String isBlack(int classId,int userId,int blackType)
    {       
        String blackInfo = "";
        List blackList = blackListDAO.listUserClass(classId,userId);
        TbbsBlackList tbbsBlackList = null;
        for(int i=0;i<blackList.size();i++)
        {
            tbbsBlackList = (TbbsBlackList)blackList.get(i);
            if(tbbsBlackList.getBlackType().intValue()==blackType)  //发现指定黑名单类型
            {
                if(tbbsBlackList.getLimitTime().intValue()==0)  //永久黑名单
                {
                    StringBuffer reason = new StringBuffer(200);
                    reason.append("您在");
                    reason.append(FormatDateTime.formatDateTime(tbbsBlackList.getCreateDatetime()));
                    reason.append("被用户(");
                    reason.append(tbbsBlackList.getOpUser());
                    reason.append(")加入了黑名单，限制理由：");
                    reason.append(tbbsBlackList.getReason());
                    blackInfo = reason.toString();
                }
                else
                {
                    int time = Utility.getBetweenMinutes(tbbsBlackList.getCreateDatetime(),new Date());
                    
                    if(time<=tbbsBlackList.getLimitTime().intValue())   //还在限制时间之内
                    {
                        StringBuffer reason = new StringBuffer(200);
                        reason.append("您在");
                        reason.append(FormatDateTime.formatDateTime(tbbsBlackList.getCreateDatetime()));
                        reason.append("被用户(");
                        reason.append(tbbsBlackList.getOpUser());
                        reason.append(")加入了黑名单，限制时间为：");
                        reason.append(tbbsBlackList.getLimitTime());
                        reason.append("分钟，限制理由：");
                        reason.append(tbbsBlackList.getReason());                    
                        blackInfo = reason.toString();
                    } 
                    else    //超过限制时间,删除该黑名单
                    {
                        blackListDAO.delete(tbbsBlackList.getBlackId());
                    }   
                }
            }    
        }    
        return blackInfo;    
    }

    public void setBlackListDAO(IBlackListDAO blackListDAO)
    {
        this.blackListDAO = blackListDAO;
    }

}
